package demo;

import ru.nsu.fit.iconveyor.conveyor.node.InnerNode;
import ru.nsu.fit.iconveyor.conveyor.node.NodeConnection;

import java.util.*;

/**
 * @author Timur Zolotuhin (tzolotuhin@gmail.com)
 */
public class DataViewerNode extends InnerNode {
    public static final String INPUT_ARG = "input_arg";
    public static final String OUTPUT_RESULT = "output_result";

    public static final String TRANSFER_ARGUMENTS_PARAMETER = "TRANSFER_ARGUMENTS_PARAMETER";

    private final NodeConnection<Object> inputArg;
    private final NodeConnection<Object> outputResult;

    public DataViewerNode(String id) {
        super(id);

        inputArg = new NodeConnection<Object>(Object.class, NodeConnection.Type.INPUT, INPUT_ARG);
        outputResult = new NodeConnection<Object>(Object.class, NodeConnection.Type.OUTPUT, OUTPUT_RESULT);

        addNodeConnection(inputArg);
        addNodeConnection(outputResult);
    }

    @Override
    public Map<String, Object> execute(Map<String, Object> inputBundles, Properties prop) {
        Map<String, Object> result = new HashMap<String, Object>();

        Object obj = getParameter(TRANSFER_ARGUMENTS_PARAMETER);
        boolean transfer = true;
        if (obj != null && obj.toString().equalsIgnoreCase("false"))
            transfer = false;

        if (inputBundles.get(INPUT_ARG) != null) {
            Object inputArgBundle = inputBundles.get(INPUT_ARG);
            if (inputArgBundle != null && inputArgBundle.getClass() != null) {
                if (inputArg.toString().length() < 100)
                    System.out.println(inputArg.toString());
                else
                    System.out.println("Long data");

                if (transfer)
                    result.put(OUTPUT_RESULT, inputArgBundle);
            }
        }

        return result;
    }

    @Override
    public Map<String, Object> preExecute(Map<String, Object> availableInputBundles, Properties prop) {
        if (availableInputBundles.get(INPUT_ARG) == null) {
            return null;
        }
        return availableInputBundles;
    }

    @Override
    public int getOutputToInputRatio() {
        //TODO: check
        return 2;
    }
}
